package com.example.helper;

import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpMessageHandler;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @Author lkb
 * @Description //
 * @Date 2019/10/29 15:39
 * @Version 1.0
 **/
@Component
@Slf4j
public class WxMessageHandler implements WxMpMessageHandler {


    @Override
    public WxMpXmlOutMessage handle(WxMpXmlMessage wxMpXmlMessage,
                                    Map<String, Object> map, WxMpService wxMpService,
                                    WxSessionManager wxSessionManager) throws WxErrorException {
        boolean isEvent = wxMpXmlMessage.getMsgType().equals("event");
        boolean isClick = isEvent && wxMpXmlMessage.getEvent().equals("CLICK");


        if (isClick) {
            //TODO 菜单点击处理
            log.debug("菜单点击事件");

        } else if (isEvent) {
            // 扫码关注
            boolean isSubmit = "subscribe".equals(wxMpXmlMessage.getEvent());
            // 已关注扫码
            boolean isScan = "SCAN".equals(wxMpXmlMessage.getEvent());
            if (isSubmit || isScan) {
                String ticket = wxMpXmlMessage.getTicket();
                if (StringUtils.isNotEmpty(ticket)) {
//                    SimpUserInfoBO simpUserInfoBO = new SimpUserInfoBO();
                    String fromUserOpenid = wxMpXmlMessage.getFromUser();
                    String appId = wxMpService.getWxMpConfigStorage().getAppId();
                    String a = "1";
//                    simpUserInfoBO.setAppid(appId);
//                    simpUserInfoBO.setOpenid(fromUserOpenid);
//                    //10 分钟有效
//                    RedisUtil.set(ticket, JSON.toJSONString(simpUserInfoBO), 60 * 10);
                }
            }
            //TODO 事件处理
            log.debug("事件推送");


        } else {
            //TODO 其他消息处理
            log.debug("其他消息");
        }
        return null;
    }
}
